package com.doupi.partner.op.controller.user;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.manager.UserRelationManager;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.model.UserRelation;
import com.doupi.partner.op.util.OpUtils;
import com.doupi.partner.service.UserRelationService;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.DataTableData;
import com.doupi.partner.util.Pagination;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 用户
 *
 *
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Controller
@RequestMapping("user/{type}")
public class UserController {
	private final static String TEMPLATE_FOLDER = "user/";

	private final UserRelationService userRelationService;

	private final UserRelationManager userRelationManager;

	@Autowired
	public UserController(UserRelationService userRelationService, UserRelationManager userRelationManager) {
		this.userRelationService = userRelationService;
		this.userRelationManager = userRelationManager;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(ModelMap modelMap, @PathVariable("type") Integer type) {
		modelMap.put("type", type);
		return TEMPLATE_FOLDER + "index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<UserRelation> list(@PathVariable("type") Byte type, Integer offset, Integer limit, UserRelation userRelation) {
		AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
		userRelation.setType(type);
		if (administratorDto.isPartner()) { // 合伙人
			PartnerRelation partnerRelation = administratorDto.getPartnerRelation();
			PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(partnerRelation.getType());
			Assert.assertNotNull(partnerType);
			switch (partnerType) {
			case HEADQUARTERS:
				break;
			case SUBSTATION:
				userRelation.setPartnerAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setSuperiorPartnerAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setCityAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setSubstationAdministratorId(partnerRelation.getAdministratorId());
				break;
			case CITY:
				userRelation.setPartnerAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setSuperiorPartnerAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setCityAdministratorId(partnerRelation.getAdministratorId());
				break;
			case SUPERIOR:
				userRelation.setPartnerAdministratorId(partnerRelation.getAdministratorId());
				userRelation.setSuperiorPartnerAdministratorId(partnerRelation.getAdministratorId());
				break;
			case SECOND:
				userRelation.setPartnerAdministratorId(partnerRelation.getAdministratorId());
				break;
			default:
				break;
			}
		}
		if (administratorDto.isBusiness()) {
			userRelation.setBusinessAdministratorId(administratorDto.getId());
		}
		Pagination<UserRelation, UserRelation> pagination = new Pagination<>(offset, limit, userRelation);
		pagination = userRelationService.queryUserRelationsWithAnnotation(pagination);
		List<UserRelation> lists = pagination.getRecords();
		// 昵称
		for (UserRelation temp : lists) {
			if (StringUtils.isNotBlank(temp.getNickname())) {
				String encodeName = new String(Base64.decodeBase64(temp.getNickname().getBytes()));
				if (StringUtils.isNotBlank(encodeName) && !StringUtils.contains(encodeName, "�")) {
					temp.setNickname(encodeName);
					Date createTime = temp.getCreateTime();
					userRelationService.updateByPrimaryKeySelective(temp);
					temp.setCreateTime(createTime);
				}
			}
		}
		// List<UserRelation> lists = userRelationManager.convertUserRelationListToDtoList(pagination.getRecords());
		return new DataTableData<>(pagination.getTotalCount(), lists);
	}
}
